<script type="text/javascript">
    $('#articles-datagrid').datagrid({
        local: 'remote',
        dataUrl: '/api/article/list',
        dataType: 'json',
        sortAll: true,
        filterAll: true,
        columns: [{
            name: 'id',
            label: '编辑',
            align: 'center',
            width:50,
            render: function(value, obj) {
                return '<a data-toggle="navtab" data-id="article_edit" data-on-close="refresh" data-title="编辑博文" href="/admin/article/edit?articleId=' + value + '">编辑</a>';
            }
        }, {
            name: 'title',
            label: '标题',
            align: 'center',
            width: 400,
            height: 40
        }, {
            name: 'excerpt',
            label: '简述',
            align: 'center',
            width: 400
        },{
            name: 'created_at',
            label: '创建时间',
            align: 'center',
            type: 'date',
            pattern: 'yyyy-MM-dd HH:mm:ss'
        }, {
            name: 'updated_at',
            label: '更新时间',
            align: 'center',
            type: 'date',
            pattern: 'yyyy-MM-dd HH:mm:ss'
        }, {
            name: 'published_at',
            label: '发布时间',
            align: 'center',
            type: 'date',
            pattern: 'yyyy-MM-dd HH:mm:ss'
        },{
            name: 'status',
            label: '状态',
            align: 'center',
            width:75,
            render: function(value) {
                if (value == "draft") {
                    return '<font color="red">未发布</font>';
                } else {
                    return '<font color="green">已发布</font>';
                }
            }
        }],
        hiddenFields: [{
            name: 'id'
        }],
        paging: {
            pageSize: 15,
            pageCurrent: 1
        },
        showCheckboxcol: true,
        linenumberAll: true,
        fullGrid: false
    });


    /**
     * 删除博文
     */
    function deleteArticle() {

        var selected = $('#articles-datagrid').data().selectedDatas;
        if (selected == undefined) {
            $(this).alertmsg('warn', '请至少选择一条记录');
            return;
        }

        var length=selected.length;
        var selectedDataIds=[];

        for(var i=0;i<length;i++){
            selectedDataIds.push(selected[i]['id']);
        }

        $(this).bjuiajax('doAjax', {
            url: '/api/article/delete',
            type: 'post',
            data: {

                articleIds:selectedDataIds.join(',')
            },
            confirmMsg: '确定要删除么?',
            callback: function(result) {
                if(result.statusCode=='200'){
                  $(this).alertmsg('info',result.message);
                  $(this).navtab('refresh');
                }
            }
        });
    }

    /**
     * 新增博文
     */
    function add(obj) {
        $(obj).navtab({
            id: 'article_edit',
            url: '/admin/article/edit',
            title: '新建博文',
            onClose: function() {
               $('#articles-datagrid').datagrid('refresh')
            }
        });
    }

    /**
     * 刷新
     */
    function refresh() {
        $('#articles-datagrid').datagrid('refresh')
    }
</script>

<div class="bjui-pageHeader">
    <div class="pull-right">
        <button type="button" class="btn btn-blue" data-icon="plus" onclick="add(this)">新建</button>
        <button type="button" class="btn btn-red" data-icon="times" onclick="deleteArticle(this)">删除</button>
        <button type="button" class="btn btn-green" data-icon="refresh" onclick="refresh()">刷新</button>
    </div>
</div>

<div class="b-jui-pageContent">

    <div>
        <table id="articles-datagrid" data-width="100%" data-height="500" class="table table-bordered">


        </table>
    </div>
</div>